package edu.hgnc.custom;


import edu.hgnc.postProcessor.CustomBeanFactoryPostProcessor;
import edu.hgnc.selfBDRPP.CustomBeanDefinitionRegisterPostProcessor;
import edu.hgnc.selfBDRPP.CustomBeanDefinitionRegisterPostProcessor2;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.util.ResourceBundle;
import java.util.logging.LogManager;

/**
 * 自定义的xml加载器
 */
public class CustomXmlLoader extends ClassPathXmlApplicationContext {

	//	打印日志
//	protected final Log logger = LogFactory.getLog(CustomXmlLoader.class);
//	protected final static Log logger2 = LogFactory.getLog(CustomXmlLoader.class);

	private static final Log logger2 = LogFactory.getLog(CustomXmlLoader.class);

	public CustomXmlLoader(String... configLocation){
		super(configLocation);
	}

	/**
	 * 自定义扩展
	 */
	@Override
	protected void initPropertySources() {
		logger2.trace("===================trace===================");
		System.out.println("======================================");
		System.out.println("==========自定义的初始化属性资源==========");
		System.out.println("======================================");
		// 获取系统环境设置必要属性值，设置值还有会有校验validate不存在系统资源的会抛出异常。
		getEnvironment().setRequiredProperties("username");
	}

	/**
	 * Customize the internal bean factory used by this context.
	 */
	@Override
	protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) {
		super.setAllowBeanDefinitionOverriding(false);
		super.setAllowCircularReferences(false);
		super.addBeanFactoryPostProcessor(new CustomBeanFactoryPostProcessor());
		// BeanDefinitionRegistryPostProcessor是BeanFactoryPostProcessor的子类，所以可以直接放进去
		super.addBeanFactoryPostProcessor(new CustomBeanDefinitionRegisterPostProcessor());
		super.customizeBeanFactory(beanFactory);
	}

	/**
	 *
	 * @param beanFactory the bean factory used by the application context
	 */
	@Override
	protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
		System.out.println("扩展实现AbstractApplicationContext类定义的postProcessBeanFactory方法");
	}
}
